package com.sgb.kjwl.view.ui.adapter.pick;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.pick.TenderAllListEntity;
import com.sgb.kjwl.model.entity.pick.TenderAllListNewEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.math.BigDecimal;
import java.util.List;

public class PickHallAllNewAdapter extends BaseRecycleAdapter<TenderAllListNewEntity> {

    private PickHomeHotChildHeadAdapter pickHomeHotChildHeadAdapter;
    private IChildClick iChildClick;

    private RecyclerView recyclerView;
    private LinearLayout noList;
    private TextView noListText;
    private ImageView imgTab;
    private LinearLayout linePrice;
    private TextView priceTitle;
    private TextView priceContent;
    private TextView tvBiddingMethod;
    private TextView jyb;
    private TextView addressContent;
    private TextView companyContent;

    public PickHallAllNewAdapter(Context context, List<TenderAllListNewEntity> datas) {
        super(context, R.layout.item_pick_hall_all_new, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, final TenderAllListNewEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        recyclerView = holder.itemView.findViewById(R.id.child_rv);
        noList = holder.itemView.findViewById(R.id.no_list);
        noListText = holder.itemView.findViewById(R.id.no_list_text);
        imgTab = holder.itemView.findViewById(R.id.img_tab);
        linePrice = holder.itemView.findViewById(R.id.line_price);
        priceTitle = holder.itemView.findViewById(R.id.price_title);
        priceContent = holder.itemView.findViewById(R.id.price_content);
        TextView tvTypeName = holder.itemView.findViewById(R.id.tv_type_name);
        TextView tvEndTime = holder.itemView.findViewById(R.id.tv_endTime);
        TextView name = holder.itemView.findViewById(R.id.name);
        TextView tvVerticalType = holder.itemView.findViewById(R.id.tv_vertical_type);
        TextView textViewWheel = holder.itemView.findViewById(R.id.tv_wheel_num);
        addressContent = holder.itemView.findViewById(R.id.address_content);
        companyContent = holder.itemView.findViewById(R.id.company_content);

        tvBiddingMethod = holder.itemView.findViewById(R.id.tv_bidding_method);
        jyb = holder.itemView.findViewById(R.id.jyb);


        if (entity.getBusinessType() == 3) {
            name.setText(entity.getProjectName());
        } else {
            name.setText(entity.getName());
        }

        if("其他".equals(entity.getTypeName())){
            tvTypeName.setBackgroundResource(R.drawable.bg_red_5);
        }else{
            tvTypeName.setBackgroundResource(R.drawable.pick_list_red);
        }
        tvTypeName.setText(entity.getTypeName());
        if(entity.getDataSources() == 2){
            addressContent.setText(TextUtils.isEmpty(entity.getFullAddress()) ? " - " :entity.getFullAddress());
            companyContent.setText(TextUtils.isEmpty(entity.getComName()) ? " - " :entity.getComName());
        }else{
            addressContent.setText(entity.getFullAddress());
            companyContent.setText(entity.getComName());
        }

        if (entity.getDataSources() == 1) {   //1.经营帮 2.政府网站
            jyb.setVisibility(View.VISIBLE);
            tvBiddingMethod.setVisibility(View.GONE);
            if (entity.getBusinessType() == 1) {   // 1.招标； 2.竞价
                // 招标方式:1、资格预审；2、资格后审；3、报价即报名
                if (entity.getQualificationWay() > 0) {
                    tvVerticalType.setVisibility(View.VISIBLE);
                } else {
                    tvVerticalType.setVisibility(View.GONE);
                }
                if (1 == entity.getQualificationWay()) {
                    tvVerticalType.setText("资格预审");
                } else if (2 == entity.getQualificationWay()) {
                    tvVerticalType.setText("资格后审");
                } else if (3 == entity.getQualificationWay()) {
                    tvVerticalType.setText("报名即报价");
                }
                switch (entity.getState()) {
                    case 2:
                    case 6:
                        tvEndTime.setText(String.format("投标结束时间:%s", TextUtils.isEmpty(entity.getBidEndTime()) ? " - " : entity.getBidEndTime()));
                        imgTab.setBackground(context.getResources().getDrawable(R.mipmap.icn_zbz));
                        break;
                    case 4:
                        tvEndTime.setText(String.format("定标时间:%s", TextUtils.isEmpty(entity.getCalibrationTime()) ? " - " : entity.getCalibrationTime()));
                        imgTab.setBackground(context.getResources().getDrawable(R.mipmap.icn_zbjs));
                        break;
                    case 9:
                        tvEndTime.setText(String.format("报名结束时间:%s", TextUtils.isEmpty(entity.getApplyEndTime()) ? " - " : entity.getApplyEndTime()));
                        imgTab.setBackground(context.getResources().getDrawable(R.drawable.pick_notice_bmz));
                        break;
                    case 3:
                        tvEndTime.setText(String.format("开标时间:%s", TextUtils.isEmpty(entity.getOpenBidTime()) ? " - " : entity.getOpenBidTime()));
                        imgTab.setBackground(context.getResources().getDrawable(R.drawable.pick_notice_pbz));
                        break;
                    case 1:
                        tvEndTime.setText(String.format("投标开始时间:%s", TextUtils.isEmpty(entity.getBidStartTime()) ? " - " : entity.getBidStartTime()));
                        imgTab.setBackground(context.getResources().getDrawable(R.drawable.pick_hall_dzb));
                        break;
                    case 8:
                        tvEndTime.setText(String.format("报名开始时间:%s", TextUtils.isEmpty(entity.getApplyStartTime()) ? " - " : entity.getApplyStartTime()));
                        imgTab.setBackground(context.getResources().getDrawable(R.drawable.pick_notice_dbm));
                        break;
                }

                //新增 “招标轮次”     “报名中”9、“待招标”1状态不显示，其他状态显示    接口判断
                if (0 == entity.getRotation() || entity.getState() == 1 || entity.getState() == 8 || entity.getState() == 9) {
                    textViewWheel.setVisibility(View.GONE);
                } else {
                    textViewWheel.setVisibility(View.VISIBLE);
                    textViewWheel.setText("第" + entity.getRotation() + "轮");
                }

                if (entity.getGoodsHeaders() == null || entity.getGoodsLists() == null) {
                    recyclerView.setVisibility(View.GONE);
                    noList.setVisibility(View.VISIBLE);
                    noListText.setText("该类型无招标清单");
                } else {
                    recyclerView.setVisibility(View.VISIBLE);
                    noList.setVisibility(View.GONE);
                    pickHomeHotChildHeadAdapter = new PickHomeHotChildHeadAdapter(context, entity.getGoodsHeaders(), entity.getGoodsLists());
                    recyclerView.setLayoutManager(new LinearLayoutManager(context));
                    recyclerView.setAdapter(pickHomeHotChildHeadAdapter);
                    pickHomeHotChildHeadAdapter.setOnItemClick(new PickHomeHotChildHeadAdapter.OnItemClick() {
                        @Override
                        public void onItemClick() {
                            iChildClick.onClick(entity);
                        }
                    });
                }
            } else if (entity.getBusinessType() == 2) {

                tvVerticalType.setVisibility(View.GONE);
                textViewWheel.setVisibility(View.GONE);
                if (2 == entity.getState()) {
                    tvEndTime.setText(String.format("截止时间:%s", TextUtils.isEmpty(entity.getApplyEndTime()) ? " - " : entity.getApplyEndTime()));
                    imgTab.setBackground(context.getResources().getDrawable(R.mipmap.icn_jjz));
                } else if (5 == entity.getState()) {
                    tvEndTime.setText(String.format("截止时间:%s", TextUtils.isEmpty(entity.getApplyEndTime()) ? " - " : entity.getApplyEndTime()));
                    imgTab.setBackground(context.getResources().getDrawable(R.mipmap.pick_tpz));
                }else if (3 == entity.getState()) {
                    tvEndTime.setText(String.format("定标时间:%s", TextUtils.isEmpty(entity.getCalibrationTime()) ? " - " : entity.getCalibrationTime()));
                    imgTab.setBackground(context.getResources().getDrawable(R.mipmap.icn_yjs));
                }

                if (entity.getGoodsHeaders() == null || entity.getGoodsLists() == null) {
                    recyclerView.setVisibility(View.GONE);
                    noList.setVisibility(View.VISIBLE);
                    noListText.setText("该类型无竞价清单");
                    if (entity.getIsDisplay() == 0) {   //0、隐藏；1、展示；
                        linePrice.setVisibility(View.GONE);
                    } else {
                        linePrice.setVisibility(View.VISIBLE);
                        if (entity.getType() == 6 || entity.getType() == 7 || entity.getType() == 8 || entity.getType() == 9 || entity.getType() == 10 || entity.getType() == 13 || entity.getType() == 20) {
                            if (entity.getRankType() == 1) {  //1：最低单价 2：最高单价  取价格的值：minMoney
                                if (TextUtils.isEmpty(entity.getMinMoney())) {
                                    priceTitle.setText("最低报价:  -");
                                    priceContent.setText("");
                                } else {
                                    priceTitle.setText("最低报价:");
                                    BigDecimal bg = new BigDecimal(entity.getMinMoney());
                                    String text = bg.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString();
                                    priceContent.setText(text + "元");
                                }
                            } else if (entity.getRankType() == 2) {
                                if (TextUtils.isEmpty(entity.getMinMoney())) {
                                    priceTitle.setText("最高报价:  -");
                                    priceContent.setText("");
                                } else {
                                    priceTitle.setText("最高报价:");
                                    BigDecimal bg = new BigDecimal(entity.getMinMoney());
                                    String text = bg.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString();
                                    priceContent.setText(text + "元");
                                }
                            }
                        }
                    }
                } else {
                    recyclerView.setVisibility(View.VISIBLE);
                    noList.setVisibility(View.GONE);
                    linePrice.setVisibility(View.GONE);
                    pickHomeHotChildHeadAdapter = new PickHomeHotChildHeadAdapter(context, entity.getGoodsHeaders(), entity.getGoodsLists());
                    recyclerView.setLayoutManager(new LinearLayoutManager(context));
                    recyclerView.setAdapter(pickHomeHotChildHeadAdapter);
                    pickHomeHotChildHeadAdapter.setDisplay(entity.getIsDisplay());
                    pickHomeHotChildHeadAdapter.setOnItemClick(new PickHomeHotChildHeadAdapter.OnItemClick() {
                        @Override
                        public void onItemClick() {
                            iChildClick.onClick(entity);
                        }
                    });
                }
            }
        } else if (entity.getDataSources() == 2) {
            jyb.setVisibility(View.GONE);
            textViewWheel.setVisibility(View.GONE);
            if(TextUtils.isEmpty(entity.getBiddingMethodName())){
                tvBiddingMethod.setVisibility(View.GONE);
            }else{
                tvBiddingMethod.setVisibility(View.VISIBLE);
                tvBiddingMethod.setText(entity.getBiddingMethodName());
            }

            // 招标方式:1、资格预审；2、资格后审；3、报价即报名
            if (entity.getQualificationWay() > 0) {
                tvVerticalType.setVisibility(View.VISIBLE);
            } else {
                tvVerticalType.setVisibility(View.GONE);
            }
            if (1 == entity.getQualificationWay()) {
                tvVerticalType.setText("资格预审");
            } else if (2 == entity.getQualificationWay()) {
                tvVerticalType.setText("资格后审");
            } else if (3 == entity.getQualificationWay()) {
                tvVerticalType.setText("报名即报价");
            }
            switch (entity.getState()) {
                case 2:
                case 6:
                    tvEndTime.setText(String.format("文件递交截止时间:%s", TextUtils.isEmpty(entity.getBidEndTime()) ? " - " : entity.getBidEndTime()));
                    imgTab.setBackground(context.getResources().getDrawable(R.mipmap.icn_zbz));
                    break;
                case 4:
                    tvEndTime.setText(String.format("文件递交截止时间:%s", TextUtils.isEmpty(entity.getBidEndTime()) ? " - " : entity.getBidEndTime()));
                    imgTab.setBackground(context.getResources().getDrawable(R.mipmap.icn_zbjs));
                    break;
                case 9:
                    tvEndTime.setText(String.format("文件获取截止时间:%s", TextUtils.isEmpty(entity.getApplyEndTime()) ? " - " : entity.getApplyEndTime()));
                    imgTab.setBackground(context.getResources().getDrawable(R.drawable.pick_notice_bmz));
                    break;
            }

            if (entity.getGoodsHeaders() == null || entity.getGoodsLists() == null) {
                recyclerView.setVisibility(View.GONE);
                noList.setVisibility(View.VISIBLE);
                noListText.setText("该类型无招标清单");
            } else {
                recyclerView.setVisibility(View.VISIBLE);
                noList.setVisibility(View.GONE);
                pickHomeHotChildHeadAdapter = new PickHomeHotChildHeadAdapter(context, entity.getGoodsHeaders(), entity.getGoodsLists());
                recyclerView.setLayoutManager(new LinearLayoutManager(context));
                recyclerView.setAdapter(pickHomeHotChildHeadAdapter);
                pickHomeHotChildHeadAdapter.setOnItemClick(new PickHomeHotChildHeadAdapter.OnItemClick() {
                    @Override
                    public void onItemClick() {
                        iChildClick.onClick(entity);
                    }
                });
            }
        }

    }

    public void onIChildClick(IChildClick iChildClick) {
        this.iChildClick = iChildClick;
    }

    public interface IChildClick {
        void onClick(TenderAllListNewEntity tenderAllListNewEntity);
    }
}
